//
//  ChapterExtension.m
//  VNBooks
//
//  Created by hainguyen on 12/17/11.
//  Copyright (c) 2011 __MyCompanyName__. All rights reserved.
//

#import "ChapterExtension.h"
#import "CoreDataHelper.h"
#import "VNBooksAppDelegate.h"

@implementation Chapter(Extension)

+ (BOOL)init{
    NSError *err;
    NSManagedObjectContext *manageObjectContext = [mainAppDelegate managedObjectContext];
    if (manageObjectContext == nil) {
        return NO;
    }
    NSString *path = [[NSBundle mainBundle] pathForResource:@"Chapters" ofType:@"plist"];
	NSMutableDictionary *chapterDict = [[NSMutableDictionary alloc] initWithContentsOfFile:path];
    NSArray *chapterList = [chapterDict objectForKey:@"Chapters"];
    for (NSDictionary *dict in chapterList) {
        Chapter *chapter = [NSEntityDescription insertNewObjectForEntityForName:@"Chapter" inManagedObjectContext:manageObjectContext];
        chapter.id = [dict objectForKey:@"id"];
        chapter.name = [dict objectForKey:@"name"];
        chapter.excerpt = [dict objectForKey:@"excerpt"];
        chapter.content = [dict objectForKey:@"content"];
        chapter.favorite = [NSNumber numberWithInt:0];
    }
    [manageObjectContext save:&err];
    
    return YES;
}

+ (BOOL)updateFavoriteStatus:(int)chapterId favorite:(BOOL)yn{
    NSError *err;
    NSManagedObjectContext *managedObjectContext = [mainAppDelegate managedObjectContext];
    if (managedObjectContext == nil)
    {
        return NO;	
    }
    Chapter *resultChapter = nil;
    NSArray *resultArray = nil;
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"id = %d", chapterId];
    @try {
        resultArray = (NSMutableArray*)[CoreDataHelper searchObjectsInContext:@"Chapter" :predicate :nil :NO :managedObjectContext];
        if ([resultArray count] != 0) {
            resultChapter = [resultArray objectAtIndex:0];
            resultChapter.favorite = [NSNumber numberWithBool:yn];
        }else{
            return NO;
        }
    }
    @catch (NSException *exception) {
        NSLog(@"Error = %@", [exception description]);
    }
    [managedObjectContext save:&err];
    
    return YES;
}

+ (NSArray*)getChapters{
    NSManagedObjectContext *managedObjectContext = [mainAppDelegate managedObjectContext];
    if (managedObjectContext == nil)
    {
        return nil;	
    }
    NSArray *resultArray = nil;
    @try 
    {
        resultArray = (NSMutableArray*) [CoreDataHelper searchObjectsInContext:@"Chapter" :nil :nil :NO :managedObjectContext];
    }
    @catch (NSException * e) 
    {
        NSLog(@"search by name error: %@", [e description]);
    }
    
    return resultArray;
}

+ (Chapter*)getChapter:(int)chapterId{
    NSManagedObjectContext *manageContext = mainAppDelegate.managedObjectContext;
    if (manageContext == nil) {
        return nil;
    }
    Chapter *resultChapter = nil;
    NSArray *resultArray = nil;
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"id = %d", chapterId];
    @try {
        resultArray = (NSMutableArray*)[CoreDataHelper searchObjectsInContext:@"Chapter" :predicate :nil :NO :manageContext];
        if ([resultArray count] != 0) {
            resultChapter = [resultArray objectAtIndex:0];
            return resultChapter;
        }
    }
    @catch (NSException *exception) {
        NSLog(@"Error = %@", [exception description]);
    }
    
    return resultChapter;
}

+ (NSArray*)searchChapterName:(NSString*)keyword{
    NSManagedObjectContext *managedObjectContext = [mainAppDelegate managedObjectContext];
    if (managedObjectContext == nil)
    {
        return nil;	
    }
    NSArray *resultArray = nil;	
    NSPredicate *predicate = [NSPredicate predicateWithFormat: @"name contains[cd] %@", keyword];
    @try 
    {
        resultArray = (NSMutableArray*) [CoreDataHelper searchObjectsInContext:@"Chapter" :predicate :nil :NO :managedObjectContext];
    }
    @catch (NSException * e) 
    {
        NSLog(@"search by name error: %@", [e description]);
    }
    
    return resultArray;
}

+ (NSArray*)searchContent:(NSString*)keyword{
    NSManagedObjectContext *managedObjectContext = [mainAppDelegate managedObjectContext];
    if (managedObjectContext == nil)
    {
        return nil;	
    }
    NSArray *resultArray = nil;	
    NSPredicate *predicate = [NSPredicate predicateWithFormat: @"content contains[cd] %@", keyword];
    @try 
    {
        resultArray = (NSMutableArray*) [CoreDataHelper searchObjectsInContext:@"Chapter" :predicate :nil :NO :managedObjectContext];
    }
    @catch (NSException * e) 
    {
        NSLog(@"search by name error: %@", [e description]);
    }
    
    return resultArray;
}

+ (BOOL)setFavoriteForChapter:(Chapter*)chapter favorite:(BOOL)yn{
    NSError *err;
    NSManagedObjectContext *manageObjectContext = mainAppDelegate.managedObjectContext;
    if (manageObjectContext == nil) {
        return NO;
    }
    Chapter *resultChapter = nil;
    NSArray *resultArray = nil;
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"id = %d", [chapter.id intValue]];
    @try {
        resultArray = (NSMutableArray*)[CoreDataHelper searchObjectsInContext:@"Chapter" :predicate :nil :NO :manageObjectContext];
        if ([resultArray count] != 0) {
            resultChapter = [resultArray objectAtIndex:0];
            resultChapter.favorite = [NSNumber numberWithBool:yn];
        }else{
            return NO;
        }
    }
    @catch (NSException *exception) {
        NSLog(@"Error = %@", [exception description]);
        return NO;
    }
    [manageObjectContext save:&err];
    
    return YES;
}

@end
